package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.BindFamilyDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老人家属Controller
 *
 * @author laoYe
 * @date 2025-10-27
 */
@RestController
@RequestMapping("/member/user")
@Slf4j
@Api(tags = "老人家属相关接口")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private IFamilyMemberElderService familyMemberElderService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IDeviceDataService deviceDataService;

    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public R login(@RequestBody UserLoginRequestDto dto) {
        LoginVo vo = familyMemberService.login(dto);
        return R.ok(vo);
    }

    /**
     * 按天统计设备数据(小时维度)
     */
    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation("按周统计查询指标数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "设备ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "functionId", value = "物理模型ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "Long", paramType = "query"),
    })
    public R<List<DeviceDataByDayVo>> queryByDay(String iotId, String functionId, Long startTime, Long endTime) {
        return R.ok(deviceDataService.statByDay(iotId, functionId, startTime, endTime));
    }

    @GetMapping("/queryDeviceDataListByWeek")
    @ApiOperation("按周统计查询指标数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "设备ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "functionId", value = "物理模型ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "Long", paramType = "query"),
    })
    public R<List<DeviceDataByWeekVo>> queryByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        return R.ok(deviceDataService.statByWeek(iotId, functionId, startTime, endTime));
    }

    /**
     * 添加家属
     */
    @PostMapping("/add")
    @ApiOperation("添加家属")
    public R login(@RequestBody BindFamilyDto dto) {
        familyMemberElderService.add(dto);
        return R.ok();
    }

    @ApiOperation("查询我的家人（老人）列表")
    @GetMapping("/my")
    public R<List<FamilyMemberElder>> getMyElders() {
        List<FamilyMemberElder> elders = familyMemberElderService.getMyElders();
        log.info("查询到的老人列表: {}", elders);
        return R.ok(elders);
    }

    @GetMapping("list-by-page")
    @ApiOperation("查看已绑定家人列表（分页查询)")
    public R<List<FamilyElderVo>> listByPage(Integer pageNum, Integer pageSize) {
        List<FamilyElderVo> result = familyMemberElderService.listByPage(pageNum, pageSize);
        return R.ok(result);
    }

    /**
     * 查询健康数据
     */
    @GetMapping("queryServiceProperties/{iotId}")
    @ApiOperation("查询健康数据")
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable String iotId) {
        List<DeviceShadowDataVo> voList = deviceService.queryServiceProperties(iotId);
        return R.ok(voList);
    }

}


